﻿#include "LunarMonth.h"
#include "LunarUtil.h"
#include "LunarYear.h"
#include "Solar.h"
#include "Lunar.h"


LunarMonth::LunarMonth(int lunarYear, int lunarMonth, int dayCount, double firstJulianDay, int index)
{
	Year = lunarYear;
	Month = lunarMonth;
	DayCount = dayCount;
	FirstJulianDay = firstJulianDay;
	Index = index;
	ZhiIndex = (index - 1 + LunarUtil::BASE_MONTH_ZHI_INDEX) % 12;
}


const LunarMonth* LunarMonth::FromYm(int lunarYear, int lunarMonth)
{

	return LunarYear::FromYear(lunarYear).GetMonth(lunarMonth);
}

bool LunarMonth::Leap()
{
	return Month < 0;
}

int LunarMonth::GanIndex()
{
	auto offset = (LunarYear::FromYear(Year).GanIndex + 1) % 5 * 2;
	return (Index - 1 + offset) % 10;
}

std::string LunarMonth::Gan()
{
	return  LunarUtil::GAN[GanIndex() + 1];
}

std::string LunarMonth::Zhi()
{
	return  LunarUtil::ZHI[ZhiIndex + 1];
}


std::string  LunarMonth::GanZhi()
{
	return   Gan() + Zhi();
}

std::string LunarMonth::PositionXi()
{
	return  LunarUtil::POSITION_XI[GanIndex() + 1];
}

std::string LunarMonth::PositionXiDesc()
{
	return LunarUtil::POSITION_DESC.at(PositionXi());
}


std::string LunarMonth::PositionYangGui()
{
	return LunarUtil::POSITION_YANG_GUI[GanIndex() + 1];
}

std::string  LunarMonth::PositionYangGuiDesc()
{
	return  LunarUtil::POSITION_DESC.at(PositionYangGui());
}


std::string LunarMonth::PositionYinGui()
{
	return	LunarUtil::POSITION_YIN_GUI[GanIndex() + 1];
}


std::string LunarMonth::PositionYinGuiDesc()
{
	return  LunarUtil::POSITION_DESC.at(PositionYinGui());
}

std::string  LunarMonth::PositionFu()
{
	return GetPositionFu();
}

std::string LunarMonth::GetPositionFu(int sect)
{
	return (1 == sect ? LunarUtil::POSITION_FU : LunarUtil::POSITION_FU_2)[GanIndex() + 1];
}

std::string LunarMonth::PositionFuDesc()
{
	return GetPositionFuDesc();
}

std::string LunarMonth::GetPositionFuDesc(int sect)
{
	return LunarUtil::POSITION_DESC.at(GetPositionFu(sect));
}

std::string LunarMonth::PositionCai()
{
	return  LunarUtil::POSITION_CAI.at(GanIndex() + 1);
}


std::string LunarMonth::PositionCaiDesc()
{
	return LunarUtil::POSITION_DESC.at(PositionCai());
}

std::string  LunarMonth::PositionTaiSui()
{

	std::string p;
	auto m = std::abs(Month);
	switch (m)
	{
	case 1:
	case 5:
	case 9:
		p = "艮";
		break;
	case 3:
	case 7:
	case 11:
		p = "坤";
		break;
	case 4:
	case 8:
	case 12:
		p = "巽";
		break;
	default:
		p = LunarUtil::POSITION_GAN[Solar::FromJulianDay(FirstJulianDay).lunar().MonthGanIndex];
		break;
	}
	return p;

}




